﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.ComponentModel;

namespace MLY.Bll
{
    [DataObject]
    public class clsMessageForOrg
    {
        clsMessageRelationsContacts msgRl;
        clsMessage msg;

        public clsMessageForOrg(clsMessageRelationsContacts msgRl, clsMessage tmpMsg)
        {
            this.msgRl = msgRl;
            this.msg = tmpMsg;
        }

        public int OrgId
        {
            get { return this.msgRl.GetOrgId; }
        }
        public string OrgName
        {
            get { return this.msgRl.GetOrgName; }
        }
        public int MsgId
        {
            get { return this.msg.Data.MsgId; }
        }

        public string DateCreated { get { return this.msg.Data.DateCreated.Value.ToShortDateString(); } }


        public string MsgTitle
        {
            get { return this.msg.Data.MsgTitle; }
        }
        public string MsgText
        {
            get { return this.msg.Data.MsgText; }
        }
        public string MsgTextShort
        {
            get
            {
                if (this.msg.Data.MsgText.Length > 100)
                {
                    return this.msg.Data.MsgText.Substring(0,97) + "...";
                }
                else
                {
                    return this.msg.Data.MsgText;
                }
            }
        }


        public static List<clsMessageForOrg> GetMessages(int OrgId)
        {
            using (Dal.lqKbmDataContext db = new MLY.Dal.lqKbmDataContext(g.AppConnectionString))
            {
                //select all organization messages
                List<Dal.TableMessage> OrgMsgList = (from o in db.TableMessages
                                                     where (o.OrganizationId == OrgId) && (bool)o.DisplayMsg && o.ExpirationDate >= DateTime.Today
                                                     select o).ToList<Dal.TableMessage>();

                //select all messages from other organizations
                List<Dal.TableMessage> OrgRecievedMsgList = (from msgRl in db.TableMessagesRelations
                                                             join msg in db.TableMessages
                                                             on msgRl.MsgId equals msg.MsgId
                                                             where (msgRl.OrganizationId == OrgId) && (bool)msg.DisplayMsg && msg.ExpirationDate >= DateTime.Today
                                                             select msg).ToList<Dal.TableMessage>();

                List<Dal.TableMessage> unionMsgList = OrgMsgList.Union(OrgRecievedMsgList).ToList();

                List<Dal.TableOrganization> orgList = (from p in db.TableOrganizations select p).ToList();

                Dictionary<int, string> orgDict = new Dictionary<int, string>();

                foreach (Dal.TableOrganization d in orgList)
                    orgDict.Add(d.OrganizationId, d.OrganizationName);

                List<clsMessageForOrg> l1 = new List<clsMessageForOrg>();
                foreach (Dal.TableMessage record in unionMsgList)
                {
                    clsMessageForOrg c = new clsMessageForOrg(new clsMessageRelationsContacts((int)record.OrganizationId, orgDict[(int)record.OrganizationId])
                                                            , new clsMessage(record.MsgId));
                    l1.Add(c);
                }
                return l1;

            }
        }
    }

    [DataObject]
    public class clsMessageRelationsContacts
    {

        int OrgId;
        string OrgName;
        bool display;

        public bool GetDisplay
        {
            set { this.display = value; }
            get { return this.display; }
        }
        public int GetOrgId
        {
            set { this.OrgId = value; }
            get { return this.OrgId; }
        }
        public string GetOrgName
        {
            set { this.OrgName = value; }
            get { return this.OrgName; }
        }
        public clsMessageRelationsContacts(int orgId, string orgName)
        {
            GetOrgId = orgId;
            GetOrgName = orgName;
        }

        public clsMessageRelationsContacts(int orgId, string orgName, bool display)
        {
            GetOrgId = orgId;
            GetOrgName = orgName;
            GetDisplay = display;
        }

        public static List<Bll.clsMessageRelationsContacts> GetOrgMessageList(int OrgId, int MsgId)
        {
            MLY.Dal.lqKbmDataContext dt = new MLY.Dal.lqKbmDataContext();
            List<Bll.clsMessageRelationsContacts> OrgContractorAndClientsList = clsOrganizationRelation.GetOrgContractorAndClients(OrgId);

            List<int> q =
                (from msg in dt.TableMessages
                 join msgRl in dt.TableMessagesRelations
                 on msg.MsgId equals msgRl.MsgId
                 where msg.MsgId == MsgId && msg.OrganizationId == OrgId && msgRl.DisplayMsg == true
                 select msgRl.OrganizationId).ToList();

            List<Bll.clsMessageRelationsContacts> OrgContractorAndClientsMessageList = new List<clsMessageRelationsContacts>();

            foreach (clsMessageRelationsContacts tmp in OrgContractorAndClientsList)
            {
                if (q.Contains(tmp.OrgId))
                {
                    OrgContractorAndClientsMessageList.Add(new Bll.clsMessageRelationsContacts(tmp.OrgId, tmp.OrgName, true));
                }
                else
                {
                    OrgContractorAndClientsMessageList.Add(new Bll.clsMessageRelationsContacts(tmp.OrgId, tmp.OrgName, false));
                }
            }
            return OrgContractorAndClientsMessageList;
        }

    }



    [DataObject]
    public class clsMessageRelation
    {
        #region Constructors
        // ==================================
        public clsMessageRelation()
        {
            _db = new Dal.lqKbmDataContext(g.AppConnectionString);
            _DataRow = new Dal.TableMessagesRelation();
            IsExist = false;
        }

        public clsMessageRelation(int MsgId, clsMessageRelationsContacts msgRlList)
        {
            _db = new Dal.lqKbmDataContext(g.AppConnectionString);
            _DataRow = new Dal.TableMessagesRelation();
            _DataRow.MsgId = MsgId;
            _DataRow.OrganizationId = msgRlList.GetOrgId;
            _DataRow.DisplayMsg = true;
            IsExist = false;

        }

        public clsMessageRelation(int MsgId)
        {
            _db = new Dal.lqKbmDataContext(g.AppConnectionString);
            _DataRow = (from o in _db.TableMessagesRelations
                        where (o.MsgId == MsgId)
                        select o).First();

            IsExist = true;
        }
        // ==================================
        #endregion

        #region Static Members
        // ==================================           
        #endregion

        #region Public Methods
        // ==================================



        public void Save()
        {
            if (IsExist)
            {
                _db.SubmitChanges();
                _DataRow.UpdateDate = DateTime.Now;
                _DataRow.UpdatedBy = clsUser.CurrentUserId;
            }
            else
            {
                _db.Refresh(System.Data.Linq.RefreshMode.KeepChanges, _db.TableMessagesRelations);
                _DataRow.DateCreated = DateTime.Now;
                _DataRow.UpdateDate = DateTime.Now;
                _DataRow.UpdatedBy = clsUser.CurrentUserId;

                _db.TableMessagesRelations.InsertOnSubmit(_DataRow);
                _db.SubmitChanges();
            }
        }
        // ==================================
        #endregion

        #region Public Properties
        // ==================================
        public Dal.TableMessagesRelation Data
        {
            get { return _DataRow; }
        }
        // ==================================
        #endregion

        #region Fields
        // ==================================
        public bool IsExist = false;
        Dal.lqKbmDataContext _db = null;
        Dal.TableMessagesRelation _DataRow = null;
        // ==================================
        #endregion
    }

}